(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.settlementInitChangeAmtController", settlementInitChangeAmtController);
	settlementInitChangeAmtController.$inject = [ "$scope", "$q", "$uibModalInstance", "dialogsModule", "modalService", "layoutConfig", "validatorConfig", "authService", "arithService",
			"formatService", "ajaxServiceFactory" ];
	function settlementInitChangeAmtController($scope, $q, $uibModalInstance, dialogsModule, modalService, layoutConfig, validatorConfig, authService, arithService, formatService, ajaxServiceFactory) {
		var ctrl = this, ajaxService = ajaxServiceFactory.buildAjaxService({
			getFeeCodeInfo : {
				url : "do/cda/feeCode/feeCodeInfo",
				method : "post"
			}
		});

		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.changeAmt = ctrl.changeAmt || {};

		// validate the input
		function validate(val) {
			var b = true;
			if (!isWhitespace(val + "") && ctrl.changeAmt.crDr == "C" && ctrl.changeAmt.feeNewFlag == "Y" && ctrl.changeAmt.feeId != "") {
				if (arithService.gt(val, formatService.parseNumber(ctrl.changeAmt.limtAmt))) {
					modalService.openErrorDialogs({
						"messages" : [ "js.pub.error.less-than", ctrl.changeAmt.limtAmt ]
					});
					b = false;
				}
			}
			return b;
		}

		/*
		 * return true if null or empty or white character @param s - a string
		 * Example: isWhitespace(priceid.value)
		 */
		function isWhitespace(s) {
			var whitespace = " \t\n\r";
			var i;
			if (angular.isUndefined(s))
				return true;
			for (i = 0; i < s.length; i++) {
				var c = s.charAt(i);
				if (whitespace.indexOf(c) == -1)
					return false;
			}
			return true;
		}

		ctrl.ok = function() {
			var newInputAmt = ctrl.changeAmt.newAmt;
			if (!validate(newInputAmt)) {
				return;
			}
			if (newInputAmt > 0) {
				var param = new Object();
				param.taxAmt = ctrl.changeAmt.taxAmt;
				param.feeAmt = ctrl.changeAmt.newAmt;
				if (param.taxAmt) {
					param.returnAmt = arithService.calc(param.feeAmt, param.taxAmt, "+");
				} else {
					param.returnAmt = param.feeAmt;
				}
				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](param) !== false) {
					$uibModalInstance.close();
				}
			} else {
				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"]() !== false) {
					$uibModalInstance.close();
				}
			}
		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.newAmtChanged = function() {
			// recalculate TaxAmt
			var taxRate = ctrl.changeAmt.taxRate;
			var feeAmt = ctrl.changeAmt.newAmt;
			var newTaxAmt = 0;
			if (ctrl.changeAmt.feeTaxFlag == "Y") {
				newTaxAmt = arithService.calc(feeAmt, arithService.calc(taxRate, 100, "/"), "*");
			}
			ctrl.changeAmt.taxAmt = newTaxAmt;
		};

		ctrl.init = function() {
			if (ctrl.dialogsModule.params.mObj) {
				ctrl.changeAmt = ctrl.dialogsModule.params.mObj;
			}
			var itemCode = ctrl.changeAmt.itemCode;
			if (itemCode) {
				ajaxService.getFeeCodeInfo({
					feeCode : itemCode
				}).then(function(response) {
					if (response.data && response.data.feeTypeObj) {
						ctrl.changeAmt.feeTaxFlag = response.data.feeTypeObj.feeTaxFlag;
					}
				});
			}
		};
	}
})(angular);
